package com.qiwenshare.ufopdemo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.ufop.constant.UploadFileStatusEnum;
import com.qiwenshare.ufop.factory.UFOPFactory;
import com.qiwenshare.ufop.operation.upload.Uploader;
import com.qiwenshare.ufop.operation.upload.domain.UploadFile;
import com.qiwenshare.ufop.operation.upload.domain.UploadFileResult;
import com.qiwenshare.ufopdemo.dto.UploadFileDTO;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


public class FileController {
    @Resource
    UFOPFactory ufopFactory;

    /**
     * 普通上传
     * @param request
     * @return
     */
    public RestResult upload1(HttpServletRequest request) {
        Uploader uploader = ufopFactory.getUploader();
        List<UploadFileResult> uploadFileResultList = uploader.upload(request);
        // multipart请求本身支持多文件上传，这里做了支持，因此返回来的是个list
        for (int i = 0; i < uploadFileResultList.size(); i++){
            UploadFileResult uploadFileResult = uploadFileResultList.get(i);

            if (UploadFileStatusEnum.SUCCESS.equals(uploadFileResult.getStatus())){
                // TODO 上传成功

            } else if (UploadFileStatusEnum.FAIL.equals(uploadFileResult.getStatus())) {
               // TODO 失败
            }
        }
        return RestResult.success();

    }

    /**
     * 分片上传
     * @param request
     * @return
     */
    public RestResult upload2(HttpServletRequest request, UploadFileDTO uploadFileDTO) {
        Uploader uploader = ufopFactory.getUploader();
        UploadFile uploadFile = new UploadFile();
        BeanUtil.copyProperties(uploadFileDTO, uploadFile);
        List<UploadFileResult> uploadFileResultList = uploader.upload(request, uploadFile);
        // multipart请求本身支持多文件上传，UFOP这里做了支持，因此返回来的是个list
        for (int i = 0; i < uploadFileResultList.size(); i++){
            UploadFileResult uploadFileResult = uploadFileResultList.get(i);

            if (UploadFileStatusEnum.SUCCESS.equals(uploadFileResult.getStatus())){
                // TODO 上传成功

            } else if (UploadFileStatusEnum.UNCOMPLATE.equals(uploadFileResult.getStatus())) {
                // TODO 未完成

            } else if (UploadFileStatusEnum.FAIL.equals(uploadFileResult.getStatus())) {
                // TODO 失败
            }
        }
        return RestResult.success();

    }
}
